//
//  B_ChangeDateFooterView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/20.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ChangeDataFooterView.h"
#import "B_ReleaseImageCVC.h"

@implementation B_ChangeDataFooterView

+ (instancetype) initB_ChangeDataFooterView {
    B_ChangeDataFooterView * mainView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
    [mainView setView];
    return mainView;
}

- (void) setView {
    [self.B_collectionView registerNib:[UINib nibWithNibName:@"B_ReleaseImageCVC" bundle:nil] forCellWithReuseIdentifier:@"B_ReleaseImageCVC"];
}



- (void)setImageArray:(NSArray *)imageArray {
    _imageArray = imageArray;
    [self.B_collectionView reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf(weSelf, self);
    B_ReleaseImageCVC * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"B_ReleaseImageCVC" forIndexPath:indexPath];
    if (self.imageArray.count >= 8) {
        cell.B_deleteButton.hidden = NO;
        if ([self.imageArray[indexPath.row] isKindOfClass:[NSString class]]) {
            [cell.B_ImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[indexPath.row]]];
        } else {
            cell.B_ImageView.image = self.imageArray[indexPath.row];
        }
    } else {
        if (self.imageArray.count == indexPath.row) {
            cell.B_ImageView.image = [UIImage imageNamed:@"B_release_topic_addimages"];
            cell.B_deleteButton.hidden = YES;
        } else {
            cell.B_deleteButton.hidden = NO;
            if ([self.imageArray[indexPath.row] isKindOfClass:[NSString class]]) {
                [cell.B_ImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[indexPath.row]]];
            } else {
                cell.B_ImageView.image = self.imageArray[indexPath.row];
            }
        }
    }
    cell.deleteImageAction = ^{
        if (weSelf.deleteAction) {
            weSelf.deleteAction(indexPath);
        }
    };
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.imageArray.count >= 8) {
        return 8;
    }
    return self.imageArray.count + 1;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.collectionTapAction) {
        self.collectionTapAction(indexPath);
    }
}

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width;
    if (self.count == 0) {
        width = (SCREEN_WIDTH - 50) / 4.;
    } else {
        width = (SCREEN_WIDTH - 50) / self.count;
    }
    return CGSizeMake(width, width + 1);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
